package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.Book;
import com.ruoyi.system.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BorrowRecordInfo;
import com.ruoyi.system.mapper.BorrowRecordMapper;
import com.ruoyi.system.domain.BorrowRecord;
import com.ruoyi.system.service.IBorrowRecordService;

/**
 * 借阅记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-22
 */

@Service
public class BorrowRecordServiceImpl implements IBorrowRecordService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询借阅记录
     *
     * @param id 借阅记录主键
     * @return 借阅记录
     */
    @Override
    public BorrowRecord selectBorrowRecordById(Integer id) {
        return borrowRecordMapper.selectBorrowRecordById(id);
    }

    /**
     * 查询借阅记录列表
     *
     * @param borrowRecord 借阅记录
     * @return 借阅记录
     */
    @Override
    public List<BorrowRecord> selectBorrowRecordList(BorrowRecord borrowRecord) {
        return borrowRecordMapper.selectBorrowRecordList(borrowRecord);
    }

    /**
     * 新增借阅记录
     *
     * @param borrowRecord 借阅记录
     * @return 结果
     */
    @Transactional  //开启事务
    @Override
    public int insertBorrowRecord(BorrowRecord borrowRecord) {
        int rows = borrowRecordMapper.insertBorrowRecord(borrowRecord);
        insertBorrowRecordInfo(borrowRecord);
        return rows;
    }

    /**
     * 修改借阅记录
     *
     * @param borrowRecord 借阅记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBorrowRecord(BorrowRecord borrowRecord) {
        borrowRecordMapper.deleteBorrowRecordInfoByBrId(borrowRecord.getId());
        insertBorrowRecordInfo(borrowRecord);
        return borrowRecordMapper.updateBorrowRecord(borrowRecord);
    }

    /**
     * 批量删除借阅记录
     *
     * @param ids 需要删除的借阅记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBorrowRecordByIds(Integer[] ids) {
        borrowRecordMapper.deleteBorrowRecordInfoByBrIds(ids);
        return borrowRecordMapper.deleteBorrowRecordByIds(ids);
    }

    /**
     * 删除借阅记录信息
     *
     * @param id 借阅记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBorrowRecordById(Integer id) {
        borrowRecordMapper.deleteBorrowRecordInfoByBrId(id);
        return borrowRecordMapper.deleteBorrowRecordById(id);
    }

    /**
     * 新增借阅记录明细信息
     *
     * @param borrowRecord 借阅记录对象
     */
    public void insertBorrowRecordInfo(BorrowRecord borrowRecord) /*throws  Exception */{

        List<Map<String, Object>> mapList = new ArrayList<>();
        List<BorrowRecordInfo> borrowRecordInfoList = borrowRecord.getBorrowRecordInfoList();
        Integer id = borrowRecord.getId();
        if (StringUtils.isNotNull(borrowRecordInfoList)) {
            List<BorrowRecordInfo> list = new ArrayList<BorrowRecordInfo>();
            for (BorrowRecordInfo borrowRecordInfo : borrowRecordInfoList) {
                borrowRecordInfo.setBrId(id);
                list.add(borrowRecordInfo);


                /*Book book=new Book();*/
                Map<String, Object> BookMap = new HashMap<>();   //要把id和count从明细表中取出来
                Integer bookId = borrowRecordInfo.getBiBid();
                Integer bookCount = borrowRecordInfo.getBiBcount();
                BookMap.put("bookId", bookId);
                BookMap.put("bookCount", bookCount);
                mapList.add(BookMap);
            }
                if (list.size() > 0) {
                    borrowRecordMapper.batchBorrowRecordInfo(list);
                }
                List<Integer> idList = new ArrayList<>();
                for (Map<String, Object> objmap : mapList) {
                    idList.add(Integer.parseInt(objmap.get("bookId").toString()));
                }
            List<Book> bookList = bookMapper.selectBookByIds(idList);
            for(Book book:bookList) {
                for (Map<String, Object> objmap : mapList) {
                    if (book.getId() == Integer.parseInt(objmap.get("bookId").toString())) {
                       Integer templeCount= book.getInventory() - Integer.parseInt(objmap.get("bookCount").toString());
                    if(templeCount<0){
                        throw new RuntimeException("超出库存，借阅失败");
                    }else if (templeCount==0)
                    {
                        book.setBorrowStatus("1");
                    }
                    book.setInventory(templeCount);
                    }
                }
        }
            for(Book book :bookList){
                bookMapper.updateBook(book);
            }
        }
    }
}